package yt.demo.appcommondb.tools;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import yt.demo.appcommondb.vo.ConditionVO;

import java.util.List;

public class ConditionApplier {

    // 根据操作符添加查询条件
    public static <T> void applyCondition(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> column, String operator, Object value) {
        switch (operator) {
            case "eq":
                wrapper.eq(column, value);
                break;
            case "gt":
                wrapper.gt(column, value);
                break;
            case "lt":
                wrapper.lt(column, value);
                break;
            case "ge":
                wrapper.ge(column, value);
                break;
            case "le":
                wrapper.le(column, value);
                break;
            case "bet":
                if (value instanceof List<?> values) {
                    if (values.size() == 2) {
                        wrapper.between(column, values.get(0), values.get(1));
                    }
                }
                break;
            case "in":
                if (value instanceof List) {
                    wrapper.in(column, (List<?>) value);
                }
                break;
            case "not in":
                if (value instanceof List) {
                    wrapper.notIn(column, (List<?>) value);
                }
                break;
            case "like":
                if (value instanceof String) {
                    wrapper.like(column, value);
                }
                break;
            case "not like":
                if (value instanceof String) {
                    wrapper.notLike(column, value);
                }
                break;
            case "is null":
                wrapper.isNull(column);  // 判断是否为空
                break;
            case "is not null":
                wrapper.isNotNull(column);  // 判断是否非空
                break;
            default:
                throw new IllegalArgumentException("Unsupported operator: " + operator);
        }
    }

    /**
     * 递归处理子条件，并按逻辑操作符 (AND / OR) 连接
     *   List<ConditionVO<AdminUser, ?>> conditions = new ArrayList<>();
     *   conditions.add(new ConditionVO<>(AdminUser::getId, "=", 1));
     *   List<ConditionVO<AdminUser, Object>> likes = new ArrayList<>();
     *   likes.add(new ConditionVO<>(AdminUser::getPhone, "=", "444"));
     *   likes.add(new ConditionVO<>(AdminUser::getPassword, "=", "333"));
     *    conditions.add(new ConditionVO<>(likes, "OR"));
     * @param wrapper         查询构造器
     * @param subConditions   子条件列表
     * @param logicalOperator 逻辑操作符 "AND" / "OR"
     */
    public static <T> void applySubConditions(LambdaQueryWrapper<T> wrapper, List<? extends ConditionVO<T, ?>> subConditions, String logicalOperator) {
        if (subConditions != null && !subConditions.isEmpty()) {
            boolean isOr = "OR".equals(logicalOperator);
            wrapper.and(w -> {
                for (int i = 0; i < subConditions.size(); i++) {
                    ConditionVO<T, ?> condition = subConditions.get(i);
                    if (i == 0) {
                        applyCondition(w, condition.getColumn(), condition.getOperator(), condition.getValue());
                    } else {
                        // 后面的条件需要 OR 连接
                        if (isOr) {
                            w.or(w2 -> applyCondition(w2, condition.getColumn(), condition.getOperator(), condition.getValue()));
                        } else {
                            w.and(w2 -> applyCondition(w2, condition.getColumn(), condition.getOperator(), condition.getValue()));
                        }
                    }
                }
            });
        }
    }

    /**
     * 处理复杂的条件逻辑：支持嵌套的 AND/OR 条件
     *
     * @param wrapper   查询构造器
     * @param condition 当前条件
     */
    public static <T> void applyComplexCondition(LambdaQueryWrapper<T> wrapper, ConditionVO<T, ?> condition) {
        // 如果当前条件有子条件（AND/OR组合的条件）
        if (condition.hasSubConditions()) {
            // 应用子条件
            applySubConditions(wrapper, condition.getSubConditions(), condition.getLogicalOperator());
        } else {
            // 如果没有子条件，直接应用当前条件
            applyCondition(wrapper, condition.getColumn(), condition.getOperator(), condition.getValue());
        }
    }
}
